package com.guandata.cdc.model.db_type.postgres;

import lombok.Getter;

import java.util.Arrays;

/**
 * The config option scan.startup.mode specifies the startup mode for Postgres SQL CDC consumer. The valid enumerations are:
 *
 * <pre>
 * initial (default): Performs an initial snapshot on the monitored database tables upon first startup, and continue to read the replication slot.
 * latest-offset: Never to perform snapshot on the monitored database tables upon first startup, just read from the end of the replication which means only have the changes since the connector was started.
 * committed-offset: Skip snapshot phase and start reading events from a confirmed_flush_lsn offset of replication slot.
 * snapshot: Only the snapshot phase is performed and exits after the snapshot phase reading is completed.
 * </pre>
 */
@Getter
public enum PostgresStartupMode {
    INITIAL("initial"),
    LATEST_OFFSET("latest-offset"),
    COMMITTED_OFFSET("committed-offset"),
    SNAPSHOT("snapshot");

    private final String code;

    PostgresStartupMode(String code) {
        this.code = code;
    }

    public static PostgresStartupMode fromCode(String mode) {
        if(mode == null) return INITIAL;
        return Arrays.stream(values())
                .filter(x -> x.code.equalsIgnoreCase(mode))
                .findFirst()
                .orElse(INITIAL);
    }
}
